<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <div class="currentItem-form" v-cloak>
        <el-form v-loading="loading" label-position="right" label-width="130px">
            <el-form-item label="商品规格" label-position="left">
                <el-input v-model="currentItem.standards" placeholder="请输入商品规格" style="width: 400px;"></el-input>
            </el-form-item>

            <el-form-item label="商品图片" label-position="left">
                <image-picker width="250" height="250" v-model="currentItem.image"></image-picker>
                <div style="font-size: 10px; color: #999">
                    <i class="el-icon-info"></i>
                    最佳图片尺寸 宽度300px，高度300px 或同比例
                </div>
            </el-form-item>

            <el-form-item label="卡券设置" v-if="is_virtual">
                <el-select :disabled="!coupons" style="width: 200px" v-model="currentItem.coupon_config_id">
                    <el-option v-for="item in coupons" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <i class="el-icon-refresh-right" @click="loadCoupons"></i>
            </el-form-item>

            <el-form-item label="兑换积分" label-position="left">
                <el-input-number controls-position="right" style="width: 200px;" v-model="currentItem.score" :min="0"
                    :max="999999999" />
            </el-form-item>

            <el-form-item label="商品库存" label-position="left">
                <el-input-number controls-position="right" style="width: 200px;" v-model="currentItem.stock" :min="0"
                    :max="999999999" />
            </el-form-item>
            <el-form-item label="兑换金额" label-position="left">
                <el-input-number controls-position="right" style="width: 200px;" v-model="currentItem.amount" :min="0"
                    :max="999999999" />
            </el-form-item>

            <div style="display: flex;justify-content: right;">
                <el-button type="default" size="mini" @click="cancleHandle" icon="el-icon-close" :disabled="loading">取消</el-button>
                <el-button type="primary" size="mini" @click="submitHandle" icon="el-icon-check" :disabled="loading">确定</el-button>
            </div>
        </el-form>
    </div>
</template>

<script>
import { updateGoodsStandards, createGoodsStandards } from "@/api/score-good";
import { getCouponConfigList } from "@/api/coupon";
import ImagePicker from "@/components/ImagePicker.vue";
export default {
    components: { ImagePicker },
    data() {
        return {
            loading: false,
            coupons: [],
        };
    },
    props: {
        currentItem: { type: Object },
        is_virtual: { type: Boolean, default: false },
    },

    mounted: function () {
        this.loadCoupons();
    },

    computed: {},

    methods: {
        cancleHandle: function () {
            this.$emit("change", this.currentItem);
        },
        submitHandle: function () {
            // validate 
            // if (this.currentItem.name.length <= 1) {
            //     this.$message.error("分类名称不能少于2个字");
            //     return false
            // }
            var data = this.currentItem;
            if (!data.standards) {
                this.$message.error("请输入商品规格");
                return;
            }
            if (!data.image) {
                this.$message.error("请上传商品图片");
                return;
            }
            if (!data.score) {
                this.$message.error("请输入商品积分");
                return;
            }

            this.loading = true;

            if (data.id) {
                updateGoodsStandards(data).then((resp) => {
                    this.loading = false;
                    if (resp.status != 0) {
                        return;
                    }
                    this.$message.success("已保存！");
                    this.$emit("change", this.currentItem);
                });
                return
            }

            createGoodsStandards(data).then((resp) => {
                this.loading = false;
                if (resp.status != 0) {
                    return;
                }
                this.$message.success("已保存！");
                this.$emit("change", this.currentItem);
            });


            // updateGoodsStandards(this.currentItem).then((resp) => {
            //     if (resp.status != 0) {
            //         return;
            //     }
            //     this.$message.success("已保存！");
            //     this.$emit("change", this.currentItem);
            // });
        },
        loadCoupons: function () {
            getCouponConfigList({
                enable: true,
                is_expired: false,
            }).then((res) => {
                this.coupons = res.data.result;
            });
        },
    },
};
</script>

<style scoped></style>